<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">

</head>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>修改密码</legend>
        <div class="layui-field-box">
            <div class="layui-col-md3">
                <form class="layui-form">
                    <div class="demo-reg-container">
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" value="" lay-verify="required" lay-vertype="tips" lay-reqtext="请输入新密码" placeholder="新密码" autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword" lay-vertype="tips" lay-reqtext="请输入确认密码" placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-reg">确认修改！</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </fieldset>
</div>
<script src="../res/layui/layui.js"></script>
<script>
    layui.use(function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        // 自定义验证规则
        form.verify({
            // 确认密码
            confirmPassword: function (value, item) {
                var passwordValue = $('#reg-password').val();
                if (value !== passwordValue) {
                    return '两次密码输入不一致';
                }
            }
        });

        // 提交事件
        form.on('submit(demo-reg)', function (data) {
            var field = data.field; // 获取表单字段值
            // 从session缓存中取出管理员id
            let admin = sessionStorage.getItem('admin')
            // 将json对象转换为js对象
            admin = JSON.parse(admin)
            // 向字段中填充当前用户的id作为后台修改密码依据
            field['id'] = admin.id;
            // 删除多余字段
            delete field['confirmPassword']
            // 此处可执行 Ajax 等操作
            // …
            $.ajax({
                url: 'http://127.0.0.1:8080/admin/update',
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify(field),
                success: function (resp) {
                    if (resp.code === 0) {
                        layer.msg('修改成功，请重新登录！', function () {
                            sessionStorage.clear()
                            //
                            window.parent.location.href = '/login.html'
                        })
                        return
                    }
                    layer.alert(resp.msg)
                }
            });
            return false; // 阻止默认 form 跳转
        })
    })
</script>
</body>
</html>